<?php
/**
 * Created by 王跃达.
 * User: 小马出行
 * Date: 2017/8/25
 * Time: 10:07
 */

namespace my;
use think\Db;
use think\Model;


class Local
{
    /*
     * 百度转高德
     */
    public static function bd2gd($rsa)
    {
        openssl_private_decrypt(base64_decode($rsa), $local, config("private"));//私钥解密
        var_dump($local);
        $url = "http://restapi.amap.com/v3/assistant/coordinate/convert?locations=" . $local . "&coordsys=baidu&output=json&key=" . Define::GD_KEY;
        $gd_local = https_request($url);
        $result = json_decode($gd_local, true);
        return $result;
    }


    /**
     * 计算两点之间的距离
     * @param $start_local
     * @param $arrive_local
     * @return array
     */
    public static function getDistance($start_local, $arrive_local)
    {
        $url = "http://restapi.amap.com/v3/direction/driving?origin=".$start_local."&destination=".$arrive_local."&extensions=all&output=json&key=" . Define::GD_KEY;
        $gd_local = https_request($url);
        $result = json_decode($gd_local, true);
        if($result['status']==0){
            return array("status"=>0);
        }
        return array("status"=>1,"data"=>$result['route']['paths'][0]['distance']);
    }

    /**
     * @param $gd_local
     * @return array
     */
    public static function getPlace($gd_local){
        if (!empty($gd_local)) {
            $result = get_info_by_local($gd_local);
            $result = json_decode($result, true);
            if ($result['status'] == 1) {
                $info["status"] = 1;
                $info["local"] = $gd_local;
                $info['province']=$result['regeocode']['addressComponent']['province'];
                $info['city']=$result['regeocode']['addressComponent']['city'];
                $info['district']=$result['regeocode']['addressComponent']['district'];
                $info["place_name"] = $result['regeocode']['pois'][0]['name'];
                $info["name"] = $result['regeocode']['formatted_address'];
                return $info;
            }
        }
        return array("status" => 0, "msg" => "定位信息错误");

    }
        /**
         * 获取省市县的regoin的id
         * @param string 
         * @param int 
         * @return array
         */
    public function getRegion($gd_local){
        $result = get_info_by_local($gd_local);
        $result = json_decode($result, true);
        if ($result['status'] == 1) {
            $adcode  =$result['regeocode']['addressComponent']['adcode'];
            $province=$result['regeocode']['addressComponent']['province'];
            $city    =$result['regeocode']['addressComponent']['city'];
            $district=$result['regeocode']['addressComponent']['district'];
            $data["place_name"] = $result['regeocode']['formatted_address'];
            $area = Db::name('region')->where('region_code',$adcode)->field('region_id,parent_id')->find();
            $data['county_id'] = empty($district)?'':$area['region_id'];
            $data['city_id'] = empty($district)?$area['region_id']:$area['parent_id'];
            $data['province_id'] = empty($district)?$area['parent_id']:Db::name('region')->where('region_id',$area['parent_id'])->value('parent_id');
            return array("status" => 0, "msg" => "ok","data"=>$data);
        }else{
            return array("status" => 1, "msg" => "经纬度出错");
        }
    }
        /**
         * 获取所在城市的regoin的id
         * @param string 
         * @param int 
         * @return array
         */
    public static function getCityId($gd_local){
       $info = self::getPlace($gd_local);
       if($info['status'] == 1){
            $data['city_id'] = Db::name('region')->where('region_name',$info['city'])->value('region_id');
            return array("status" => 0, "msg" => "ok","data"=>$data['city_id']);
       }else{
            return array("status" => 1, "msg" => "经纬度出错");
       }

    }

    public static function post2Gd($url,$data){
        $s = "";
        ksort($data);
        foreach ($data as $k => $v) {
            $s .= $k . "=" . $v . "&";
        }
        $s = substr($s, 0, strlen($s) - 1);
        $sign = md5($s . Define::GD_SECRET);
        $r = https_request($url,$s."&sig=".$sign);
        return json_decode($r,true);
    }
}